package com.dxp.plugins;

import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.exception.BusinessException;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import net.sf.json.JSONObject;

import java.util.List;
import java.util.Map;

/**
 * @Classname CheckDelDataPluginImpl
 * @Description 校验是否满足条件删除数据
 * @Date 2020/3/20 15:30
 * @Created by fpc
 */
public class CheckDelDataPluginImpl implements CustomMethodPlugin {
    @Override
    public Object beforeInvoke(Map<String, Object> map) {
        String checkObj = String.valueOf(map.get("checkObj"));
        Map checkParam = JSONObject.fromObject(map.get("checkParam"));
        if(Validator.isNotNull(checkObj)&& !Validator.isEmpty(checkParam)){
            ResultEntity resultEntity = IusPluginUtil.getInstance().select("list", checkObj, checkParam);
            if(!Validator.isEmpty(resultEntity.getEntity())){
                List list = (List) resultEntity.getEntity();
                if(list.size()>0){
                    throw new BusinessException("F01");//存在数据不能删除，前端根据不同页面进行不同的友好提示。
                }
            }
        }else{
            throw new BusinessException("校验参数不对，请检查参数。");
        }

        return null;
    }

    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        return null;
    }
}
